package com.twl.app.base.exception;

import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import com.twl.app.base.entity.Checktemplet;

/*@Component
@Aspect*/
public class ExceptionAop {
	//final static String exception = "execution(* com..*.hobby(..))||execution(* com..*.haveFun(..))";
//	final static String exception ="execution(* com..*.ctrl.*.insert*(..))||execution(* com..*.ctrl.*.save*(..))"; 
	final static String exception ="execution(* com.twl..*Ctrl*.*(..))"; 
	//final static String after = "execution(* com..*.run(..))||execution(* com..*.hobby(..))||execution(* com..*.haveFun(..))";

	/**
	 * 定义切点
	 */
	@Pointcut(value = exception)
	private void pointCut() {
	}

	@AfterThrowing(pointcut = "pointCut()", throwing = "subclass")
	public void afterThrowing(JoinPoint jp, Throwable subclass) {
		if (subclass instanceof Exception) {
			System.out.println("类：" + jp.getTarget().getClass().getName()
					+ "   方法：" + jp.getSignature().getName() + "  参数:"
					+ Arrays.toString(jp.getArgs()));
			System.out.println(((Checktemplet)jp.getArgs()[0]).getStaffint());
			// System.err.println(getStackTrace(subclass));
			subclass.printStackTrace();
		}
	}
		
		
	@After(value = exception)
	public void after(JoinPoint jp) {
		System.out.println("#############################" + "类："
				+ jp.getTarget().getClass().getName() + "   方法："
				+ jp.getSignature().getName() + "  参数:"
				+ Arrays.toString(jp.getArgs()));
	}

	/**
	 * 获取异常信息
	 * 
	 * @param e
	 * @return
	 */
	private String getStackTrace(Throwable e) {
		StringBuffer sb = new StringBuffer();
		sb.append("\n").append(e);
		for (StackTraceElement element : e.getStackTrace()) {
			sb.append("\n").append("\tat ").append(element);
		}
		if (e.getCause() != null) {
			sb.append(getStackTraceAsCause(e));
		}
		return sb.toString();
	}

	/**
	 * 获取异常信息
	 * 
	 * @param throwable
	 * @return
	 */
	private String getStackTraceAsCause(Throwable throwable) {
		StringBuffer sb = new StringBuffer();
		Throwable throwableCause = throwable.getCause();
		StackTraceElement[] trace = throwable.getStackTrace();
		StackTraceElement[] causedTrace = throwableCause.getStackTrace();
		int m = trace.length - 1, n = causedTrace.length - 1;
		while (m >= 0 && n >= 0 && trace[m].equals(causedTrace[n])) {
			m--;
			n--;
		}
		int framesInCommon = causedTrace.length - 1 - n;
		sb.append("\nCaused by: ").append(throwableCause).append("\n");
		for (int i = 0; i <= n; i++) {
			sb.append("\tat " + causedTrace[i]);
			sb.append("\n");
		}
		if (framesInCommon != 0) {
			sb.append("\t... " + framesInCommon + " more");
		}
		Throwable ourCause = throwableCause.getCause();
		if (ourCause != null) {
			sb.append(getStackTraceAsCause(throwableCause));
		}
		return sb.toString();
	}

}
